package com.meihua.shellapp.activity.maker;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.BaseBean;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.StoreManageBean;
import com.meihua.shellapp.dialog.BaseMessageDialog;
import com.meihua.shellapp.utils.DateUtils;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import java.util.Calendar;
import java.util.Date;

import butterknife.Bind;

/**
 * 门店管理
 * 作者：lc on 2017/9/30.
 * 邮箱：lc0607live@163.com
 */

public class StoreManageActivity extends BaseActivity {

    @Bind(R.id.iv_top_left)
    ImageView iv_top_left;//返回
    @Bind(R.id.tv_title)
    TextView tv_title;//标题
    @Bind(R.id.tv_time)
    TextView tv_time;//期限
    @Bind(R.id.rg_start_time)
    RadioGroup rg_start_time;//开始筹备时间
    @Bind(R.id.tv_submit)
    TextView tv_submit;//提交

    private long startTime;
    private long endTime;
    private BaseMessageDialog dialog;
    private Calendar c;

    @Override
    public int getLayoutId() {
        return R.layout.activity_storemanage;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        iv_top_left.setVisibility(View.VISIBLE);
        iv_top_left.setOnClickListener(this);
        tv_title.setVisibility(View.VISIBLE);
        c = Calendar.getInstance();
        tv_title.setText("门店管理");
        tv_submit.setBackgroundResource(R.drawable.btn_backgroud);
        tv_submit.setOnClickListener(this);
        rg_start_time.setOnCheckedChangeListener(listener);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_submit://提交
                if (startTime == 0) {
                    ToastUtils.getInstance().showToast("请选择筹备日期！");
                    return;
                }
                submitMessage();
                break;
        }
    }

    private void submitMessage() {
        OkGo.<String>post(URL.getUrl().APPLYSTORE)
                .params("member_name", MyApplication.getInstance().bean.getName())
                .params("member_number", MyApplication.getInstance().getUserId())
                .params("branch_shop_number", MyApplication.getInstance().bean.getBranch_shop_number())
                .params("start_time", startTime)
                .params("end_time", endTime)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        tv_submit.setEnabled(false);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        BaseBean bean = MyApplication.getInstance().getBeanForJson(BaseBean.class, response.body());
                        if (bean == null) {
                            return;
                        }
                        if (bean.getCode() == 1) {
                            dialog = new BaseMessageDialog.Builder(mContext)
                                    .setCencleShow(View.GONE)
                                    .setMessage(bean.getMsg())
                                    .setClickSure(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            dialog.dismiss();
                                            finish();
                                        }
                                    })

                                    .show();
                        } else {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        tv_submit.setEnabled(true);
                    }
                });
    }


    /**
     * 单选按钮监听
     */
    private RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            switch (i) {
                case R.id.rb_A:
                    c.setTime(new Date());
                    c.add(Calendar.MONTH, 3);
                    startTime = new Date().getTime() / 1000;
                    endTime = c.getTimeInMillis() / 1000;
                    tv_time.setText("您的筹备期限为:" + DateUtils.getInstance().stampToDate6(startTime + "") + "-" + DateUtils.getInstance().stampToDate6(endTime + ""));
                    break;
                case R.id.rb_B:
                    c.setTime(new Date());
                    c.add(Calendar.MONTH, 1);
                    startTime = c.getTimeInMillis() / 1000;
                    c.add(Calendar.MONTH, 3);
                    endTime = c.getTimeInMillis() / 1000;
                    tv_time.setText("您的筹备期限为:" + DateUtils.getInstance().stampToDate6(startTime + "") + "-" + DateUtils.getInstance().stampToDate6(endTime + ""));
                    break;
            }
        }
    };

    @Override
    public void createLoadData() {
        super.createLoadData();
        getData();
    }

    private void getData() {
        OkGo.<String>post(URL.getUrl().GETSTOREMANAGE)
                .params("m_number", MyApplication.getInstance().getUserId())
                .params("member_level", MyApplication.getInstance().bean.getMember_level())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        StoreManageBean bean = MyApplication.getInstance().getBeanForJson(StoreManageBean.class, response.body());
                        if (bean == null) {
                            return;
                        }
                        String message = "";
                        if (bean.getCode() == 1) {
                            switch (bean.getData()) {
                                case "1":
                                    tv_submit.setBackgroundResource(R.drawable.btn_backgroud);
                                    break;
                                case "2"://不具备申请资格
                                    message = "对不起，您不具备申请资格";
                                    break;
                                case "3"://审核
                                    message = "正在审核中，请耐心等待...";
                                    break;
                                case "4"://未通过
                                    message = "对不起，您的审核未通过。";
                                    break;
                                case "5"://通过
                                    message = "您的审核已通过!";
                                    break;
                            }
                            if (!message.equals("")) {
                                dialog = new BaseMessageDialog.Builder(mContext)
                                        .setCencleShow(View.GONE)
                                        .setMessage(message)
                                        .isCancelTouch(false)
                                        .isCancel(false)
                                        .setClickSure(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                dialog.dismiss();
                                                finish();
                                            }
                                        })
                                        .show();
                            }
                        } else {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }
                });
    }
}
